package com.sugon.controller.forecase;

import com.sugon.controller.util.SPFD;
import com.sugon.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实时监测分析
 */
@Slf4j
@Controller
@RequestMapping("electricalSignal")
public class Forecast {
    @Autowired
    private SysConfigService sysConfigService;

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> getDatas(int type, int passageway, int page) {
        Map<String, Object> res = new HashMap<>();
        File dir = new File(sysConfigService.getForecaseDataPath());
        File[] files = dir.listFiles();
        File file = files[(page % 984) + 1];
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
            String line = null;
            List l = new ArrayList();
            while ((line = in.readLine()) != null) {
                String[] strArr = line.split("\t");
                l.add(Double.valueOf(strArr[passageway - 1].toString()));
            }
            double[] l_d = ListTodouble(l);
            double a1 = SPFD.CIb(l_d);
            res.put("a_0", a1);
            double a2 = SPFD.CLIb(l_d);
            res.put("a_1", a2);
            double a3 = SPFD.IMIb(l_d);
            res.put("a_2", a3);
            double a4 = SPFD.SIb(l_d);
            res.put("a_3", a4);
            double a5 = SPFD.KUb(l_d);
            res.put("a_4", a5);
            double a6 = SPFD.SKb(l_d);
            res.put("a_5", a6);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        res.put("name", file.getName());
        res.put("status", 1);
        return res;
    }

    /**
     * 类型值转换
     */
    private double[] ListTodouble2(List<Double> list) {
        Double[] doubles = new Double[SPFD.ntom(list.size())];
        Double[] a = list.toArray(doubles);
        double[] result = new double[doubles.length];
        for (int i = 0; i < doubles.length; i++) {
            result[i] = a[i].doubleValue();
        }
        return result;
    }

    /**
     * 类型值转换
     */
    private double[] ListTodouble(List<Double> list) {
        Double[] doubles = new Double[list.size()];
        list.toArray(doubles);
        double[] result = new double[doubles.length];
        for (int i = 0; i < doubles.length; i++) {
            result[i] = doubles[i].doubleValue();
        }
        return result;
    }

    /**
     * 历史数据分析
     */
    @RequestMapping("/distory")
    @ResponseBody
    public Map<String, Object> getDestory(int type, int passageway, int page) {
        Map<String, Object> res = new HashMap<>();
        File dir = new File(sysConfigService.getForecaseDataPath());
        File[] files = dir.listFiles();
        File file = files[(page % 356)];
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
            String line = null;
            List l = new ArrayList();
            while ((line = in.readLine()) != null) {
                String[] strArr = line.split("\t");
                l.add(Double.valueOf(strArr[passageway - 1].toString()));
            }
            res.put("datas", l);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        res.put("status", 1);
        return res;
    }

    @RequestMapping("/autoCorrelation")
    @ResponseBody
    public double[] getautoCorrelation(int passageway) {
        Map<String, Object> res = new HashMap<>();
        File dir = new File(sysConfigService.getForecaseDataPath());
        File[] files = dir.listFiles();
        File file = files[(3 % 984)];
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
            String line = null;
            List l = new ArrayList();
            while ((line = in.readLine()) != null) {
                String[] strArr = line.split("\t");
                l.add(Double.valueOf(strArr[passageway - 1].toString()));
            }
            return SPFD.autoCorrelation(ListTodouble(l), 20);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return null;
    }

    @RequestMapping("/crossCorrelation")
    @ResponseBody
    public double[] getcrossCorrelation(int passageway) {
        Map<String, Object> res = new HashMap<>();
        File dir = new File(sysConfigService.getForecaseDataPath());
        File[] files = dir.listFiles();
        File file = files[(3 % 984)];
        BufferedReader in = null;
        BufferedReader in2 = null;
        try {
            in = new BufferedReader(new FileReader(file));
            String line = null;
            List l = new ArrayList();
            while ((line = in.readLine()) != null) {
                String[] strArr = line.split("\t");
                l.add(Double.valueOf(strArr[passageway - 1].toString()));
            }
            File file2 = files[(2 % 984)];
            in2 = new BufferedReader(new FileReader(file2));
            String line2 = null;
            List l2 = new ArrayList();
            while ((line2 = in2.readLine()) != null) {
                String[] strArr = line2.split("\t");
                l2.add(Double.valueOf(strArr[passageway - 1].toString()));
            }
            return SPFD.crossCorrelation(ListTodouble(l), ListTodouble(l2), 20);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
            if (in2 != null) {
                try {
                    in2.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return null;
    }

    @RequestMapping("/fft1")
    @ResponseBody
    public Map<String, Object> fft1(int passageway) {
        Map<String, Object> result = new HashMap<>();
        File dir = new File(sysConfigService.getForecaseDataPath());
        File[] files = dir.listFiles();
        File file = files[(3 % 984)];
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
            String line = null;
            List l = new ArrayList();
            while ((line = in.readLine()) != null) {
                String[] strArr = line.split("\t");
                l.add(Double.valueOf(strArr[passageway - 1].toString()));
            }
            double[] a = new double[SPFD.ntom(l.size())];
            a = SPFD.fft1(ListTodouble2(l));
            result.put("datas", a);
            result.put("stats", 1);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }

        return result;
    }

    @RequestMapping("/hilbtb")
    @ResponseBody
    public double[] hilbtb(int passageway) {
        Map<String, Object> result = new HashMap<>();
        File dir = new File(sysConfigService.getForecaseDataPath());
        File[] files = dir.listFiles();
        File file = files[(3 % 984)];
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
            String line;
            List l = new ArrayList();
            while ((line = in.readLine()) != null) {
                String[] strArr = line.split("\t");
                l.add(Double.valueOf(strArr[passageway - 1].toString()));
            }
            return SPFD.hilbtb(ListTodouble(l));
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return null;
    }

    @RequestMapping("/filter")
    @ResponseBody
    public double[] filter(int passageway, int n, double fln, double fhn, double fs, int band) {
        Map<String, Object> result = new HashMap<>();
        double[] a = new double[n + 1];
        a = SPFD.firwin(n, fln, fhn, fs, band);
        File dir = new File(sysConfigService.getForecaseDataPath());
        File[] files = dir.listFiles();
        File file = files[(3 % 984)];
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
            String line = null;
            List l = new ArrayList();
            while ((line = in.readLine()) != null) {
                String[] strArr = line.split("\t");
                l.add(Double.valueOf(strArr[passageway - 1].toString()));
            }
            double[] b = new double[l.size()];
            for (int i = 0; i < l.size(); i++) {
                b[i] = 1;
            }
            return SPFD.filter(a, b, ListTodouble(l));
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return null;
    }

    @RequestMapping("/d1")
    @ResponseBody
    public double[] d1(int passageway) {
        Map<String, Object> result = new HashMap<>();
        File dir = new File(sysConfigService.getForecaseDataPath());
        File[] files = dir.listFiles();
        File file = files[(2 % 984)];
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
            String line;
            List l = new ArrayList();
            while ((line = in.readLine()) != null) {
                String[] strArr = line.split("\t");
                l.add(Double.valueOf(strArr[passageway - 1].toString()));
            }
            return SPFD.hilbtb(ListTodouble(l));
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return null;
    }

    @RequestMapping("/d2")
    @ResponseBody
    public double[] d2(int passageway) {
        Map<String, Object> result = new HashMap<>();
        File dir = new File(sysConfigService.getForecaseDataPath());
        File[] files = dir.listFiles();
        File file = files[(4 % 984)];
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
            String line;
            List l = new ArrayList();
            while ((line = in.readLine()) != null) {
                String[] strArr = line.split("\t");
                l.add(Double.valueOf(strArr[passageway - 1].toString()));
            }
            return SPFD.hilbtb(ListTodouble(l));
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return null;
    }

    @RequestMapping("/d3")
    @ResponseBody
    public double[] d3(int passageway) {
        Map<String, Object> result = new HashMap<>();
        File dir = new File(sysConfigService.getForecaseDataPath());
        File[] files = dir.listFiles();
        File file = files[(1 % 984)];
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
            String line;
            List l = new ArrayList();
            while ((line = in.readLine()) != null) {
                String[] strArr = line.split("\t");
                l.add(Double.valueOf(strArr[passageway - 1].toString()));
            }
            return SPFD.hilbtb(ListTodouble(l));
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return null;
    }

    @RequestMapping("/d4")
    @ResponseBody
    public double[] d4(int passageway) {
        Map<String, Object> result = new HashMap<>();
        File dir = new File(sysConfigService.getForecaseDataPath());
        File[] files = dir.listFiles();
        File file = files[(3 % 984)];
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
            String line;
            List l = new ArrayList();
            while ((line = in.readLine()) != null) {
                String[] strArr = line.split("\t");
                l.add(Double.valueOf(strArr[passageway - 1].toString()));
            }
            return SPFD.hilbtb(ListTodouble(l));
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return null;
    }
}
